package top.btcmarket.TestProject;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import top.btcmarket.DoubleUtil;




/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        
//        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";		
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{42}$";		

//		String value = "0x0000000000e00000000000000000000000000000";  // 长度不够
		String value = "0x68E7F20eF696c392ae168795A6AC3B81e89C6f48";  // 长度不够
		System.out.println(value.matches(regex));

		value = "1111aaaa1111aaaaa";  // 太长
		System.out.println(value.matches(regex));

		value = "111111111"; // 纯数字
		System.out.println(value.matches(regex));

		value = "aaaaaaaaa"; // 纯字母
		System.out.println(value.matches(regex));

		value = "####@@@@#"; // 特殊字符
		System.out.println(value.matches(regex));

		value = "1111aaaa";  // 数字字母组合
		System.out.println(value.matches(regex));

		value = "aaaa1111"; // 数字字母组合
		System.out.println(value.matches(regex));

		value = "aa1111aa";	// 数字字母组合
		System.out.println(value.matches(regex));

		value = "11aaaa11";	// 数字字母组合
		System.out.println(value.matches(regex));

		value = "aa11aa11"; // 数字字母组合
		System.out.println(value.matches(regex));
		
		
		Date date = new Date();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		
		System.out.println(sdf.format(date));
		
		
		Double d2 = new Double("1234567890.12456464564");   
		System.out.println("d:="+d2);  
		java.text.NumberFormat NF = java.text.NumberFormat.getInstance();   
		NF.setGroupingUsed(false);//去掉科学计数法显示
		System.out.println("d:="+NF.format(d2));
		
		
		Double d = new Double(12345.6789);  
		String ds = null;  
		DecimalFormat df=(DecimalFormat)DecimalFormat.getInstance();  
		df.setMaximumFractionDigits(8); //设置最多小数位数  
		df.setMinimumFractionDigits(0); //设置最少小数位数  
		ds = df.format(d); 
		System.out.println(ds);
		
		
		double value1 = 0.21000001001;  
        System.out.println(formatNum(value1));  
        System.out.println(formatDouble(value1)); 
        
        
        double dd = 0.14533331;
        DecimalFormat df1 = new DecimalFormat("##.00000000");
        String str = df1.format(dd);
        System.out.println(str);
        
        System.out.println("--------"+"1.00000001".replaceAll("\\.", ""));
        System.out.println("--------"+"0.00000001".replaceAll("\\.", ""));
        
        String format = String.format(formatNum(0.1*100000000));
        String format11 = String.format(formatNum(0.00000001*100000000));
        String format12 = String.format(formatNum(1.00000001*100000000));
        String format13 = String.format(formatNum(222.1*100000000));
        String format14 = String.format(formatNum(5555.00000007*100000000));
        
        System.out.println(format11);
        System.out.println(format12);
        System.out.println(format13);
        System.out.println(format14);
        
        String format1 = String.format("%.8f", 0.00000001);
        String format2 = String.format("%.8f", 1.00000001);
        String format3 = String.format("%.8f", 222.1);
        String format4 = String.format("%.8f", 5555.00000007);
        System.out.println(format);
        System.out.println(format1);
        System.out.println(format2);
        System.out.println(format3);
        System.out.println(format4);
        
        Integer  gas_limit = Integer.parseInt("80000")+35000*100;
        System.out.println(""+gas_limit);
        
        
        BigDecimal number= new BigDecimal(5555.00000007);
		BigDecimal mul= new BigDecimal(100000000);
		long longValue = number.multiply(mul).longValue();
		System.out.println(longValue);
		
		
		   BigDecimal bd = new BigDecimal(0.12345676);
	        bd = bd.setScale(8, BigDecimal.ROUND_HALF_DOWN);
//	        bd = bd.setScale(8, BigDecimal.ROUND_DOWN);  // 不一样
	        System.out.println(bd.toString());
	        
	        
	        BigDecimal errorMethod = new BigDecimal(17196.6);
	        System.out.println("errorMethod: "+errorMethod.toString());
	        
	        BigDecimal errorMethod1 = new BigDecimal(Double.toString(17196.6));
	        System.out.println("errorMethod: "+errorMethod1.toString());
	        
	        
	        BigDecimal bd1 = new BigDecimal(17196.62345675);
//	        bd1 = bd1.setScale(8, BigDecimal.ROUND_HALF_DOWN);
//	        bd1 = bd1.setScale(8, BigDecimal.ROUND_DOWN);
	        bd1 = bd1.setScale(8, BigDecimal.ROUND_HALF_EVEN);
	        System.out.println("---bd1-----"+bd1.toString());
	        
	        
	        BigDecimal bd3 = new BigDecimal(1.5);
	        bd3 = bd3.setScale(8, BigDecimal.ROUND_HALF_DOWN);
	        System.out.println("--------"+bd3.toString());
	        
	        
	        
	        BigDecimal bd2 = new BigDecimal(Double.toString(17196.6));
	        bd2 = bd2.setScale(8, BigDecimal.ROUND_HALF_DOWN);
	        System.out.println("--------"+bd2.toString());
	        
	        String address = "0x8513b0419f20b7d0234b137c4aff031afd4516ff#0.12345678,0x314872ea6b3f59164876e6825774468c5b9b99e4#10.12345678,0x16c2024233b926e6c5906ad2be02b38693449225#5,0xa7c39663c040feba8cd9e385e7b0118f23915421#3.85,";
			if(address.endsWith(",")){
				System.out.println(address.substring(0, address.length()-1));
			}
			
			String fee = "10.31256548665";
//			String fee = "120.3454";
			System.out.println(fee.indexOf("."));
			if(fee.indexOf(".")>0){
				String[] numberSplit = fee.split("\\.");
				String subStr = numberSplit[1];
				System.out.println("-------"+subStr);
				if(subStr.length()>8){
					subStr = subStr.substring(0,8) ;
					System.out.println("-------"+subStr.substring(0,8));
				}else{
					System.out.println("-------"+subStr);
				}
				
				System.out.println(numberSplit[0]+"."+subStr);
				
			}
			
			System.out.println(truncateDouble("10.23446886454",8));
			System.out.println(truncateDouble("0.23446886454",8));
			System.out.println(truncateDouble("10",8));
			System.out.println(truncateDouble("10.234",8));
			System.out.println(truncateDouble("454510.23446886454",8));
			
			Integer dd11 = new Integer(1586);
			Integer dd12 = new Integer(1586);
			
			System.out.println(dd11.equals(dd12));
			System.out.println(dd11.intValue() == dd12.intValue());
			
			BigDecimal bd1eee = new BigDecimal("0.24500");
			BigDecimal bd1ee1e = new BigDecimal("0.24");
			
			System.out.println(bd1eee.compareTo(bd1ee1e));
			
			Integer ddddd= new Integer(-2);
			System.out.println(ddddd.equals(-1));
			
			Long ssddsds = 454658765456l;
			System.out.println(ssddsds.toString());
			
			
			System.out.println(8679187740293466377L%3);
			String key = "0xe5ae36d8e27e16dfcd2d2d9728063bf1f9e4f22d8f21f54c0777afea1d7721a7";
			if(key.startsWith("0x")){
				System.out.println(key.substring(2, key.length()));
				System.out.println(key.substring(2));
			}
			
			String ss = "{\"code\":\"0000000000\",\"message\":null,\"validMsg\":{},\"data\":{\"size\":4826,\"pageCount\":2413,\"cntPerPage\":2,\"pageIndex\":1,\"blockModel\":null,\"accountAddr\":\"0x55a57bcf1242bf2a54216dbbf4d792a6dac0eddb\",\"list\":[{\"hash\":null,\"blockHeight\":null,\"blockHash\":null,\"transactionIndex\":null,\"gasPrice\":null,\"gas\":null,\"publicKey\":null,\"blockTimestamp\":null,\"created\":null,\"updated\":null,\"totalVaule\":null,\"bcTransferDOList\":[{\"hash\":\"0xa061b6c272120cf8412cdba6be779e138b79b911fbd91cb1ce0ad38bce24a1a2\",\"blockHeight\":5447405,\"blockHash\":\"0xf4680c7c0d0401818429c4ef7540c1df25ccea03cb40382734a7b0cf082d4f03\",\"transactionIndex\":52,\"fromAddr\":\"0x55a57bcf1242bf2a54216dbbf4d792a6dac0eddb\",\"toAddr\":\"0x05745c6ef312534d35c25b90b544de4507b059d0\",\"value\":224763079640,\"blockTimestamp\":1523830644000,\"created\":1523830656000,\"updated\":1523830656000},{\"hash\":\"0xa061b6c272120cf8412cdba6be779e138b79b911fbd91cb1ce0ad38bce24a1a2\",\"blockHeight\":5447405,\"blockHash\":\"0xf4680c7c0d0401818429c4ef7540c1df25ccea03cb40382734a7b0cf082d4f03\",\"transactionIndex\":52,\"fromAddr\":\"0x55a57bcf1242bf2a54216dbbf4d792a6dac0eddb\",\"toAddr\":\"0x9033c6f1e846543edfd941f1cd9b579e9357beb9\",\"value\":282744001046,\"blockTimestamp\":1523830644000,\"created\":1523830656000,\"updated\":1523830656000}],\"preHash\":\"\",\"nextHash\":\"\"}]}}";
			JSONObject parseObject = JSONObject.parseObject(ss);
			if(parseObject != null){
				JSONObject jsonObject = parseObject.getJSONObject("data");
				System.out.println(jsonObject.toString());
				JSONArray jsonArray = jsonObject.getJSONArray("list");
				System.out.println(jsonArray.toString());
				
				/*JSONObject parseObject2 = JSONObject.parseObject(jsonArray.toString());
				JSONArray jsonArray2 = parseObject2.getJSONArray("bcTransferDOList");*/
//				jsonArray2.forEach(action);
				
			}
			
			Map<String,Object> map = (Map<String,Object>)JSON.parse(ss);
			showMap(map);
			Map<String,Object> dataMap=(Map<String, Object>) map.get("data");  
			showMap(dataMap);
			List<Map<String,Object>> fileList= (List<Map<String, Object>>) dataMap.get("list");  
			for(Map<String,Object> soj:fileList){  
	            System.out.println(soj);  
	            Map<String,Object> detailData=soj;  
	            showMap(detailData);  
	            System.out.println("----------------------------------------"); 
	            
	            List<Map<String,Object>> fileList1 = (List<Map<String, Object>>)detailData.get("bcTransferDOList");
	            for (Map<String, Object> map2 : fileList1) {
	            	 System.out.println(map2);  
				}
	            
	        } 
			 System.out.println("--------ddddddddddddd--------------------------------"); 
			
			List<Map<String,Object>> transInfoData = getTransInfoData(ss);
			for (Map<String, Object> map2 : transInfoData) {
				showMap(map2); 
			}
			
			BigDecimal dddee = new BigDecimal("10000000000");
			
			BigDecimal ddd = new BigDecimal("52715");
			BigDecimal ddd1 = new BigDecimal("52714");
			BigDecimal bigInteger = ddd.divide(ddd1,10,RoundingMode.DOWN);
			long bigInteger2 = ddd.divide(ddd1,10,RoundingMode.DOWN).multiply(dddee).longValue();
			System.out.println("bigInteger:"+bigInteger);
			System.out.println("bigInteger2:"+bigInteger.multiply(dddee).longValue());
			System.out.println("bigInteger3:"+bigInteger2);
			
			System.out.println("5023".startsWith("4"));
			
			
			
			String ddaa = "{\"address\":\"1BkAbua1pp7ZuJFuUK7tUQyxcZTfQLUnZm\",\"transactions\":[{\"from\":[{\"address\":\"1BkAbua1pp7ZuJFuUK7tUQyxcZTfQLUnZm\",\"value\":\"6411300\"}],\"to\":[{\"address\":\"1PtvNPdPvTqQPAzm5cUAwpnue1SNDM4Tum\",\"value\":\"1000000\"},{\"address\":\"1BQ2sVYehveq6RecV51iLtVaLteBM5f1D8\",\"value\":\"5410170\"}],\"timestamp\":\"1520682123\",\"hash\":\"5473244986df383fa121c9e96c0a9f71440c9e6124c197eb3c71d6567e95f702\",\"receive\":\"-6411300\"},{\"from\":[{\"address\":\"174ta15XjBRwEdJfiUJSW2oETzavYbZHTt\",\"value\":\"100000000\"}],\"to\":[{\"address\":\"1BkAbua1pp7ZuJFuUK7tUQyxcZTfQLUnZm\",\"value\":\"6411300\"},{\"address\":\"1JRWpYiVvXfg9xGYHF9BhD1yp9NXqreYd1\",\"value\":\"93419200\"}],\"timestamp\":\"1520682080\",\"hash\":\"0a03f4a335a8edfe730b0cc322a168cbc52f1b38f43adbfcdcb46c9dca574f68\",\"receive\":\"6411300\"}]}";
			JSONObject parseObjectrrr = JSONObject.parseObject(ddaa);
			
			JSONArray jsonArray = parseObjectrrr.getJSONArray("transactions");
			for (int i = 0; i < jsonArray.size(); i++) {
				
				JSONObject arr = jsonArray.getJSONObject(i);
				
				System.out.println("timestamp---"+arr.getString("timestamp"));
				System.out.println("hash---"+arr.getString("hash"));
				System.out.println("receive---"+arr.getString("receive"));
				
				String from = arr.getString("from") ;
				String to = arr.getString("to") ;
				
				System.out.println("from---"+from);
				System.out.println("to---"+to);
				
				System.out.println("from is :"+from.indexOf("1BkAbua1pp7ZuJFuUK7tUQyxcZTfQLUnZm"));
				System.out.println("to is :"+to.indexOf("1BkAbua1pp7ZuJFuUK7tUQyxcZTfQLUnZm"));
				
				
//				BigDecimal  dddd = new BigDecimal("8919.76111111");
				BigDecimal  dddd = new BigDecimal("8919.76");
				BigDecimal  dddd6 = new BigDecimal("100000000");
				
				
				String truncateDouble = DoubleUtil.truncateDouble(dddd.toPlainString(), 2);
				
				System.out.println("truncateDouble:"+truncateDouble);
				
				
				BigDecimal divide = new BigDecimal(truncateDouble);
				System.out.println(divide.multiply(dddd6).setScale(0));
				
				String memberIds = "1231";
				 String[] ids = memberIds.split(",");
				
				System.out.println(Arrays.toString(ids));
				
				BigDecimal ddrr = new BigDecimal("1.");
				System.out.println(ddrr.toPlainString());
				
			}
			
			BigDecimal minValue = new BigDecimal("0.00000545") ;
			System.out.println(validateSendMinBalance(minValue,CoinTypeProtocol.BTC.getProtocol()));
			
			
			System.out.println(GetUTCTimeUtil.getUTCTimeStr());
			System.out.println(GetUTCTimeUtil.getUtcTime());
			
			Integer ddd3 = new Integer(2587);
			System.out.println(ddd3 != 2587);
			
			Calendar cal = Calendar.getInstance(java.util.Locale.CHINA);
			System.out.println("local:"+cal.getTime());
			
			Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
			System.out.println("UTC:"+cal1.getTime());
			
			System.out.println("getChinaTime:"+getChinaTime());
			System.out.println("getChinaTime:"+getChinaDateTime());
			
			
			Map<String,Object> params = new HashMap<>();
			params.put("groupId", 123456456);
			params.put("type", "1");
			params.put("coinId", 22);
			params.put("coinName", "ETH");
			
			JSONObject jsonObject = null ;
	    	if(params != null){
	   		 	jsonObject = JSONObject.parseObject(JSONObject.toJSONString(params));
	   		 	
	   		 	Long groupId = jsonObject.getLong("groupId");
				//0:普通，1:随机
				Integer type = jsonObject.getInteger("type");
				Integer coinId = jsonObject.getInteger("coinId");
				String coinName = jsonObject.getString("coinName");
				
				System.out.println("groupId:"+groupId+",type"+type+",coinId="+coinId+",coinName:"+coinName);
				
	    	}
			
			

			
					
    }
    
    private static boolean validateSendMinBalance(BigDecimal balance, String protocol) {
		// TODO Auto-generated method stub
		boolean flag = false ;
		if(balance != null && StringUtils.isNotBlank(protocol)){
			BigDecimal minValue = null ;
			if(CoinTypeProtocol.LTC.getProtocol().equals(protocol)){
				minValue = new BigDecimal("0.000546");
			}else if(CoinTypeProtocol.BTC.getProtocol().equals(protocol)){
				minValue = new BigDecimal("0.00000546");
			}else if(CoinTypeProtocol.BCH.getProtocol().equals(protocol)){
				minValue = new BigDecimal("0.00000546");
			}
			if(minValue != null){
				if(balance.compareTo(minValue)<0){
					flag = true ;
				}
			}
		}
		return flag;
	}
    
    private static List<Map<String,Object>> getTransInfoData(String data) {
		
		if(StringUtils.isNotEmpty(data)){
			Map<String,Object> map = (Map<String,Object>)JSON.parse(data);
			if(map != null){
				Map<String,Object> dataMap=(Map<String, Object>) map.get("data"); 
				if(dataMap != null){
					List<Map<String,Object>> fileList= (List<Map<String, Object>>) dataMap.get("list");  
					if(fileList != null && fileList.size()>0){
						Map<String, Object> map3 = fileList.get(0);
						if(map3 != null){
							return  (List<Map<String, Object>>)map3.get("bcTransferDOList");
						}
					}
				}
			}
		}
		return null ;
	}
    
    /*** 
     * 
      * @param map 需要打印的Map结构 
     */  
    public static void showMap(Map<String,Object> map){  
        for(Map.Entry<String,Object> kv:map.entrySet()){  
            System.out.println(kv.getKey()+"  "+kv.getValue());  
        }  
    }  
    
    /**
     * 0.23446886454  -> 0.23446886
     * 10.23446886454  ->  10.23446886
     * 10 -> 10
     * @param doubleStr
     * @param dotLen
     * @return
     */
    public static String truncateDouble(String doubleStr,int dotLen){
    	StringBuffer sb = new StringBuffer();
    	if(doubleStr != null && doubleStr.trim().length()>0){
    		int len = doubleStr.indexOf(".");
    		if(len>0){
    			String[] numberSplit = doubleStr.split("\\.");
				String subStr = numberSplit[1];
				if(subStr.length()>dotLen){
					subStr = subStr.substring(0,dotLen);
				}
				
				sb.append(numberSplit[0]).append(".").append(subStr);
    		}
    	}
    	
    	if(sb.length()<=0){
    		sb.append(doubleStr);
    	}
    	
    	return sb.toString() ;
    }
    
    @Test
    public void testBig(){
        System.out.println(0.11+2001299.32);//非精确的输出
        BigDecimal bigDecimal1 = new BigDecimal(Double.toString(0.11));
        BigDecimal bigDecimal2 = new BigDecimal(Double.toString(2001299.32));
        System.out.println(bigDecimal1.add(bigDecimal2));//精确的输出
    }
    
    public static String formatNum(double value) {  
        String retValue = null;  
        DecimalFormat df = new DecimalFormat();  
        df.setMinimumFractionDigits(0);  
        df.setMaximumFractionDigits(8);  
        retValue = df.format(value);  
        retValue = retValue.replaceAll(",", "");  
        return retValue;  
    }  
  
    public static String formatDouble(double value) {  
        String retValue = null;  
        NumberFormat format = NumberFormat.getInstance();  
        format.setMinimumFractionDigits(0);  
        format.setMaximumFractionDigits(8);  
        format.setGroupingUsed(false);  
        retValue = format.format(value);  
        retValue = retValue.replaceAll(",", "");  
        return retValue;  
    }  
    
    
    /**
	* 获得东八区时间
	*
	* @return
	*/
	public static String getChinaTime() {
		TimeZone timeZone = TimeZone.getTimeZone("GMT+8:00");
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		simpleDateFormat.setTimeZone(timeZone);
		
		return simpleDateFormat.format(new Date());
	}
	
	/**
	* 获得东八区时间
	*
	* @return
	*/
	public static Date getChinaDateTime() {
		TimeZone timeZone = TimeZone.getTimeZone("GMT+8:00");
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		simpleDateFormat.setTimeZone(timeZone);
		return simpleDateFormat.getCalendar().getTime();
		//return simpleDateFormat.format(new Date());
	}
    
    
    


}
